/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dusin.wufang.ui;

import com.mxgraph.model.mxCell;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import org.dusin.wufang.util.jgraph.MyJGraphUtil;

/**
 *
 * @author dusin
 */
class GraphMouseHandler implements MouseListener {

   mxGraph graph;
    private mxGraphComponent graphComponent;

    public GraphMouseHandler(mxGraph graph) {
        this.graph = graph;
        this.graphComponent = new mxGraphComponent(graph);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        mxCell cell = (mxCell) graphComponent.getCellAt(e.getX(), e.getY());
        
        if (cell != null) {
            System.out.println("cell=" + graph.getLabel(cell));
            System.out.println("cell=" + cell.toString());
            //System.out.println("***" + cell.getStyle());
            String componentName = MyJGraphUtil.getValue(cell.getStyle(), "shape");
            System.out.println(MyJGraphUtil.getValue(cell.getStyle(), "shape"));
            if("hDaozha".equalsIgnoreCase(componentName)||"vDaozha".equalsIgnoreCase(componentName)){
                String strIsOpen = MyJGraphUtil.getValue(cell.getStyle(), "isOpen");
                //System.out.println("&&&&&&&"+strIsOpen);
                graph.getModel().beginUpdate();
                if("false".equalsIgnoreCase(strIsOpen)){
                    cell.setStyle(MyJGraphUtil.setValue(cell.getStyle(), "isOpen", "true"));
                    
                }else{
                    cell.setStyle(MyJGraphUtil.setValue(cell.getStyle(), "isOpen", "false"));
                }
                graph.getModel().endUpdate();
            }
            
//                                        if(cell.isVertex()){
//                                            
//                                            jgraph.getModel().beginUpdate();
//                                            if(isopen)
//                                                cell.setStyle("shape=myThreeC;isOpen=false");
//                                            else
//                                                cell.setStyle("shape=myThreeC;isOpen=true");
//                                            graphComponent.refresh();
//                                            jgraph.getModel().endUpdate();
//                                        }
            System.out.println(cell.getClass());
        }
        graph.refresh();
        System.out.println("released");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
    
}
